﻿using DataScripter.Database;
using DataScripter.Scripting;
using DataScripter.Scripting.Filters;
using DataScripter.Scripting.Fluent;
using DataScripter.Scripting.InputSources;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;

namespace DataScripter.UnitTests.FluentScriptRequestTests
{
    [TestClass]
    public class WhenSpecifyingAWhereClause
    {
        private const string THIS_IS_MY_WHERE_CLAUSE = "ThisIsMyWhereClause";
        private readonly IFluentTableScriptRequest _fluentTableRequest;
        private readonly IFluentTableScriptRequest _result;

        public WhenSpecifyingAWhereClause()
        {
            Engine engine = new Engine();
            IInputSource inputSource = new SqlServerInputSource("", engine);
            engine.RegisterType(inputSource);
            Mock<IDatabaseStructure> databaseStructureMock = new Mock<IDatabaseStructure>();
            databaseStructureMock.Setup(ds => ds.GetTable("MyTable")).Returns((ITableStructure) null);
            ITableScriptRequest tableScriptRequest = new TableScriptRequest("MyTable", databaseStructureMock.Object, 0);
            _fluentTableRequest = new FluentTableScriptRequest(null, tableScriptRequest, engine.Resolve<IInputSource>());
            _result = _fluentTableRequest.Where(THIS_IS_MY_WHERE_CLAUSE);
        }

        [TestMethod]
        public void WhereClauseShouldBeRecorded()
        {
            Assert.IsTrue(_fluentTableRequest.Request.Filters.ContainsKey("WHERE"));
        }

        [TestMethod]
        public void RequestShouldGetWhereFilter()
        {
            Assert.IsTrue(_fluentTableRequest.Request.Filters["WHERE"] is IWhereFilter);
        }

        [TestMethod]
        public void WhereFilterShouldContainWhereClause()
        {
            IWhereFilter filter = (IWhereFilter) _fluentTableRequest.Request.Filters["WHERE"];
            Assert.AreEqual(THIS_IS_MY_WHERE_CLAUSE, filter.WhereClause);
        }

        [TestMethod]
        public void ResultAndOriginalFluentRequestAreTheSame()
        {
            Assert.AreSame(_fluentTableRequest, _result);
        }
    }
}
